# -*- coding: utf-8 -*-
from django.test import TestCase
from apps.member.models import Member
from apps.member.forms import SignupForm, LoginForm
import apps.member.test_helper as helper
from django.core.urlresolvers import reverse

class MemberTest(TestCase):
    def test_save_member_though_form(self):
            form = SignupForm({ 'email': 'some@domain.com', 'password1': '1', 'password2': '1' })
            member = form.save()
            self.assertTrue(isinstance(member, Member))
            self.assertTrue(member.user.id)

    def test_save_member_though_form_with_nickname(self):
        form = SignupForm({ 'email': 'some@domain.com', 'nickname': u'田乐', 'password1': '1', 'password2': '1' })
        member = form.save()
        self.assertTrue(member.user.id)

    def test_save_member_should_show_error_when_password_not_matching(self):
        form = SignupForm({ 'email': 'some@domain.com', 'password1': '1', 'password2': '2' })
        self.assertFalse(form.save())
        self.assertEquals(1, len(form.errors))

    def test_save_member_should_show_error_when_email_is_not_valid(self):
        form = SignupForm({ 'email': 'domain.com', 'password1': '1', 'password2': '1' })
        self.assertFalse(form.save())
        self.assertEquals(1, len(form.errors))

    def test_save_member_should_show_error_when_usernick_valid(self):
        form = SignupForm({ 'email': 'domain.com', 'password1': '1', 'nickname': '123456', 'password2': '1' })
        self.assertFalse(form.save())
        self.assertEquals(1, len(form.errors))
        form = SignupForm({ 'email': 'some@domain.com', 'password1': '1', 'nickname': 'Bei Jing', 'password2': '1' })
        self.assertFalse(form.save())
        self.assertEquals(1, len(form.errors))

    def test_login(self):
        helper.create_user()
        response = self.client.post(reverse('login'), {'email': 'tin@domain.com', 'password': '123'})
        self.assertRedirects(response, '/')
    
    def test_login_should_failed_when_password_is_wrong(self):
        helper.create_user()
        response = self.client.post(reverse('login'), {'email': 'tin@domain.com', 'password': 'wrong-password'})
        self.assertFormError(response, 'form', '', u'您输入的邮件地址与密码不匹配或者帐号还不存在，请您重试或者注册帐号')
    
    def test_login_should_failed_before_activate(self):
        helper.create_user(activate=False)
        response = self.client.post(reverse('login'), {'email': 'tin@domain.com', 'password': '123'})
        self.assertFormError(response, 'form', '', u'您还没有通过邮件激活帐号，请您登陆邮箱打开链接激活')

    def test_avatar_of_member(self):
        member = helper.create_user()
        user = member.user
        self.assertEquals('http://www.gravatar.com/avatar.php?default=http%3A%2F%2Fapp.beijing-open-party.org%2Fmedia%2Fimages%2Fdefault_gravatar.png&size=40&gravatar_id=ea746490cff50b7d53bf78a11c86815a', user.get_profile().avatar)

    def test_find_member_by_email(self):
        member = helper.create_user()
        user = member.user
        found = Member.objects.find_by_email(user.email)
        self.assertEquals(member, found)

    def test_find_member_by_none_existing_email(self):
        not_found = Member.objects.find_by_email('iamnotexisting@gmail.com')
        self.assertFalse(not_found)

